package com.zhijian.warehouse.manage.service.impl;

import com.zhijian.util.BeanUtil;
import com.zhijian.util.DateUtil;
import com.zhijian.util.MapBuilderUtil;
import com.zhijian.warehouse.manage.entity.export.*;
import com.zhijian.warehouse.manage.entity.vo.request.*;
import com.zhijian.warehouse.manage.entity.vo.response.StoreInProductDetail;
import com.zhijian.warehouse.manage.entity.vo.response.StoreOutProductDetail;
import com.zhijian.warehouse.manage.entity.vo.response.StoreStockRecordProductVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreStockRecordVo;
import com.zhijian.warehouse.manage.service.*;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import com.zhijian.warehouse.manage.util.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通道门设置业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-11-03 15:31:18
 */
@Slf4j
@Service
public class ExcelExportServiceImpl implements ExcelExportService {
    private final String ACCESS_PRODUCT_NAME = "仪器领用归记录.xlsx";
    private final String ACCESS_PRODUCT_ILLEGAL_NAME = "违规领用记录.xlsx";
    private final String ACCESS_PRODUCT_OVERDUE_NAME = "仪器超期未归还记录.xlsx";
    private final String ACCESS_PRODUCT_FACILITY_NAME = "通道门标签感应记录.xlsx";
    private final String ACCESS_PRODUCT_STORE_NAME = "库存查询.xlsx";
    private final String ACCESS_PRODUCT_IN_STORE_NAME = "产品入库.xlsx";
    private final String ACCESS_PRODUCT_OUT_STORE_NAME = "产品出库.xlsx";
    private final String ACCESS_PRODUCT_OPT_STORE_NAME = "产品出入库查询.xlsx";
    private final String SHEET_NAME = "sheet1";
    @Autowired
    private AccessProductRecordService accessProductRecordService;
    @Autowired
    private AccessFacilityRecordService accessFacilityRecordService;
    @Autowired
    private StoreRecordService storeRecordService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private StoreOptRecordService storeOptRecordService;

    @Autowired
    private StoreOutRecordService storeOutRecordService;

    @Autowired
    private StoreStockRecordService storeStockRecordService;

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void accessProductExport(AccessProductRecordSearchVo param, HttpServletResponse response) {
        List<AccessProductRecordExport> accessProductRecordVos = accessProductRecordService.selectExportList(param);
        accessProductRecordVos = accessProductRecordVos == null ? new ArrayList<>() : accessProductRecordVos;
        ExcelUtils.exportExcelXssf(accessProductRecordVos, null, SHEET_NAME, AccessProductRecordExport.class, ACCESS_PRODUCT_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void illegalExport(AccessFacilityRecordSearchVo param, HttpServletResponse response) {
        List<AccessIllegalRecordExport> accessFacilityRecordVos = accessFacilityRecordService.selectIllegalExportList(param);
        accessFacilityRecordVos = accessFacilityRecordVos == null ? new ArrayList<>() : accessFacilityRecordVos;
        ExcelUtils.exportExcelXssf(accessFacilityRecordVos, null, SHEET_NAME, AccessIllegalRecordExport.class, ACCESS_PRODUCT_ILLEGAL_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void overdueExport(AccessProductRecordSearchVo param, HttpServletResponse response) {
        List<AccessOverdueRecordExport> accessOverdueRecordExports = accessProductRecordService.selectOverdueExportList(param);
        accessOverdueRecordExports = accessOverdueRecordExports == null ? new ArrayList<>() : accessOverdueRecordExports;
        ExcelUtils.exportExcelXssf(accessOverdueRecordExports, null, SHEET_NAME, AccessOverdueRecordExport.class, ACCESS_PRODUCT_OVERDUE_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void accessFacilityExport(HttpServletResponse response, AccessFacilityRecordSearchVo param) {
        List<AccessFacilityRecordExportVo> accessFacilityRecordVos = accessFacilityRecordService.selectFacilityList(param);
        accessFacilityRecordVos = accessFacilityRecordVos == null ? new ArrayList<>() : accessFacilityRecordVos;
        ExcelUtils.exportExcelXssf(accessFacilityRecordVos, null, SHEET_NAME, AccessFacilityRecordExportVo.class, ACCESS_PRODUCT_FACILITY_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void storeInExport(HttpServletResponse response, StoreRecordSearchVo param) {
        List<StoreInExport> storeInExports = storeRecordService.selectStoreInExportList(param);
        if (BeanUtil.isNotEmpty(storeInExports)) {
            storeInExports.forEach(storeRecordVo -> {
                // 拼接字段
                StringBuilder concatenatedFields = new StringBuilder();
                if (BeanUtil.isNotEmpty(storeRecordVo.getProductDetailList())) {
                    storeRecordVo.getProductDetailList().forEach(storeInProductDetail -> {
                        concatenatedFields.append(storeInProductDetail.getProductNumber()).append("-");
                        concatenatedFields.append(storeInProductDetail.getProductName()).append("-");
                        concatenatedFields.append(storeInProductDetail.getProductModel()).append("-");
                        concatenatedFields.append(storeInProductDetail.getStoreCount()).append(";");
                    });
                    storeRecordVo.setProductDetail(concatenatedFields.toString());
                    //获取仓库名称并去重后以都逗号分隔
                    List<String> warehouseNames = storeRecordVo.getProductDetailList().stream().map(StoreInProductDetail::getWarehouseName).distinct().collect(Collectors.toList());
                    storeRecordVo.setWarehouseName(String.join(",", warehouseNames));
                    storeRecordVo.setProductDetailList(null);
                }
            });
        }
        storeInExports = storeInExports == null ? new ArrayList<>() : storeInExports;
        ExcelUtils.exportExcelXssf(storeInExports, null, SHEET_NAME, StoreInExport.class, ACCESS_PRODUCT_IN_STORE_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void storeOutExport(HttpServletResponse response, StoreOutRecordSearchVo param) {
        List<StoreOutRecordExport> storeOutRecordExports = storeOutRecordService.selectStoreOutExportList(param);
        storeOutRecordExports = storeOutRecordExports == null ? new ArrayList<>() : storeOutRecordExports;
        storeOutRecordExports.forEach(storeRecordVo -> {
            // 拼接字段
            StringBuilder concatenatedFields = new StringBuilder();
            if (BeanUtil.isNotEmpty(storeRecordVo.getProductDetailList())) {
                storeRecordVo.getProductDetailList().forEach(storeInProductDetail -> {
                    concatenatedFields.append(storeInProductDetail.getProductNumber()).append("-");
                    concatenatedFields.append(storeInProductDetail.getProductName()).append("-");
                    concatenatedFields.append(storeInProductDetail.getProductModel()).append("-");
                    concatenatedFields.append(storeInProductDetail.getStoreCount()).append(";").append(System.lineSeparator());
                });
                storeRecordVo.setProductDetail(concatenatedFields.toString());
                //获取仓库名称并去重后以都逗号分隔
                List<String> warehouseNames = storeRecordVo.getProductDetailList().stream().map(StoreOutProductDetail::getWarehouseName).distinct().collect(Collectors.toList());
                storeRecordVo.setWarehouseName(String.join(",", warehouseNames));
                storeRecordVo.setProductDetailList(null);
            }
        });
        ExcelUtils.exportExcelXssf(storeOutRecordExports, null, SHEET_NAME, StoreOutRecordExport.class, ACCESS_PRODUCT_OUT_STORE_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void storeOptExport(HttpServletResponse response, StoreOptRecordSearchVo param) {
        List<StoreOptRecordExport> storeOptRecordExports = storeOptRecordService.selectStoreOptExportList(param);
        storeOptRecordExports = storeOptRecordExports == null ? new ArrayList<>() : storeOptRecordExports;
        ExcelUtils.exportExcelXssf(storeOptRecordExports, null, SHEET_NAME, StoreOptRecordExport.class, ACCESS_PRODUCT_OPT_STORE_NAME, response);
    }

    @Override
    public void storeOptDetailExport(HttpServletResponse response, StoreOptRecordSearchVo param) {
        List<StoreOptDetRecordExport> storeOptDetailExport = storeOptRecordService.storeOptDetailExport(param);
        storeOptDetailExport = storeOptDetailExport == null ? new ArrayList<>() : storeOptDetailExport;
        ExcelUtils.exportExcelXssf(storeOptDetailExport, null, SHEET_NAME, StoreOptDetRecordExport.class, ACCESS_PRODUCT_OPT_STORE_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void storeExport(HttpServletResponse response, StoreSearchVo param) {
        List<StoreExport> storeExports = storeService.selectStoreExportList(param);
        if (BeanUtil.isNotEmpty(param.getExportStartTime()) && BeanUtil.isNotEmpty(param.getExportEndTime())) {
            param.setExportStartTime(param.getExportStartTime() + " 00:00:00");
            param.setExportEndTime(param.getExportEndTime() + " 23:59:59");
        }
        storeExports = storeExports == null ? new ArrayList<>() : storeExports;
        ExcelUtils.exportExcelXssf(storeExports, null, SHEET_NAME, StoreExport.class, ACCESS_PRODUCT_STORE_NAME, response);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public void storeStockDetailExport(HttpServletResponse response, StoreStockRecordSaveOrUpdateVo param) {
        storeStockRecordService.modify(param);
        AssertCommonUtil.check(BeanUtil.isEmpty(param.getId()), "参数错误，id不能为空");
        StoreStockRecordVo storeStock = storeStockRecordService.selectVoById(param.getId());
        Map<String, Object> data = MapBuilderUtil.builder().build();
        data.put("number", storeStock.getNumber());
        data.put("stockTime", storeStock.getStockTime());
        data.put("stockUserName", storeStock.getStockUserName());
        data.put("warehouseName", storeStock.getWarehouseName());
        data.put("createUserName", storeStock.getCreateUserName());
        data.put("createTime", storeStock.getCreateTime());
        data.put("audiUserName", storeStock.getAudiUserName());
        data.put("auditTime", storeStock.getAuditTime());
        data.put("stockTypeStr", storeStock.getStockTypeStr());
        data.put("note", storeStock.getNote());

        List<Map<String, Object>> list = new ArrayList<>();
        if (BeanUtil.isNotEmpty(storeStock.getStoreStockRecordProductList()))
            for (StoreStockRecordProductVo storeStockRecordProduct : storeStock.getStoreStockRecordProductList()) {
                Map<String, Object> build = MapBuilderUtil.builder().build();
                build.put("rowSize", 1);//标识
                build.put("productNumber", storeStockRecordProduct.getProductNumber());
                build.put("productName", storeStockRecordProduct.getProductName());
                build.put("productRfId", storeStockRecordProduct.getProductRfId());
                build.put("categoryName", storeStockRecordProduct.getCategoryName());
                build.put("productModel", storeStockRecordProduct.getProductModel());
                build.put("unitName", storeStockRecordProduct.getUnitName());
                build.put("storeCount", storeStockRecordProduct.getStoreCount());
                build.put("stockCount", storeStockRecordProduct.getStockCount());
                build.put("deficitNumber", storeStockRecordProduct.getDeficitNumber());
                list.add(build);
            }
        data.put("list", list);
        ExcelUtils.exportExcelWithFtl("盘点单导出" + DateUtil.getTime(DateUtil.DATE_FORMAT_SYMD),
                MapBuilderUtil.builder().put("data", data).build(), "ep_store_stock_new.ftl", response);

    }
}
